<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Models\Order;
class OrderController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $columns = ['订单编号','下单时间','收货地址','收货人','联系电话','状态','备注'];
        return $this->index_model_list(Order::class,$columns,$request,function($builder) use($request){

            $keyword = $request->get('keyword','');
            $status = $request->get('status',0);

            //自定义查询条件
            $builder->where(['status'=>$status]);
            if(!empty($keyword)){
                $builder->where(function($q) use($keyword){
                    $q->where('order_id','like','%'.$keyword.'%')
                            ->orWhere('consignee','like','%'.$keyword.'%')
                            ->orWhere('mobile','like','%'.$keyword.'%')
                            ->orWhere('remark','like','%'.$keyword.'%');
                });
            }
        },function($model) {
            //导出字段定义
            return [
               $model->order_id,
               $model->order_date,
               $model->addres,
               $model->consignee,
               $model->mobile,
               $model->getStatusText(),
               $model->remark
           ];
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Order::findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
